const { dbName, http } = require("../modal/db.config");
const SECRET = dbName;
const { getCurrentDateTime } = require("../modal/time.config");
const STATE = {
  LOW: 0,
  UP: 1,
};
const resolveDataFun = (req, list) => {
  return new Promise((resolve, reject) => {
    let p = list.map(async (v) => {
      if (v.features) v.features = JSON.parse(v.features);
      if (v.carousel) v.carousel = JSON.parse(v.carousel);
      return v;
    });
    Promise.all(p).then((res) => {
      resolve(res);
    });
  });
};
exports.create = async (req, res) => {
  const body = req.body;
  const {
    title,
    price,
    content,
    address,
    cover,
    features,
    carousel,
    styleImg,
    propertySize,
    propertyId,
    bedrooms,
    bathrooms,
    hasGarage,
    yearBuilt,
    state = STATE.LOW,
  } = body;
  const data = {
    title,
    price,
    content,
    address,
    cover,
    features: JSON.stringify(features),
    carousel: JSON.stringify(carousel),
    styleImg,
    propertySize,
    propertyId,
    bedrooms,
    bathrooms,
    hasGarage,
    yearBuilt,
    state,
    createTime: getCurrentDateTime(),
    updateTime: getCurrentDateTime(),
  };
  const result = await req.services.HousesService.create(data);
  if (result.affectedRows === 1) {
    res.status(200).json({ data: "新增成功", msg: "success", code: 200 });
  } else {
    res.status(200).json({ data: "系统错误，请重试", msg: "fail", code: 500 });
  }
};

exports.update = async (req, res) => {
  const body = req.body;
  const {
    title,
    price,
    address,
    cover,
    features,
    carousel,
    styleImg,
    propertySize,
    propertyId,
    bedrooms,
    bathrooms,
    hasGarage,
    yearBuilt,
    state,
    content,
    id,
  } = body;
  const data = {
    title,
    price,
    address,
    cover,
    features: JSON.stringify(features),
    carousel: JSON.stringify(carousel),
    styleImg,
    propertySize,
    propertyId,
    bedrooms,
    bathrooms,
    hasGarage,
    yearBuilt,
    state,
    content,
    updateTime: getCurrentDateTime(),
  };
  const newData = await req.services.HousesService.update(data, { id });
  if (newData.affectedRows === 1) {
    res.status(200).json({ data: "更新成功", msg: "success", code: 200 });
  } else {
    res.status(200).json({ data: "系统错误，请重试", msg: "fail", code: 500 });
  }
};

exports.getDetail = async (req, res) => {
  const body = req.query;
  const { id } = body;
  const newData = await req.services.HousesService.findById(id);
  const resolveData = await resolveDataFun(req, [newData]);
  if (!id) {
    res.status(200).json({ data: '系统错误，请检查参数', msg: "fail", code: 500 });
    return
  }
  if (newData) {
    res.status(200).json({ data: resolveData[0], msg: "success", code: 200 });
  } else {
    res.status(200).json({ data: "查无数据", msg: "fail", code: 400 });
  }
};
exports.list = async (req, res) => {
  const body = req.body;
  const {
    title = "",
    state = "",
    propertyId,
    page = null,
    pageSize = null,
  } = body;
  let newData = await req.services.HousesService.getList({
    pageSize: pageSize,
    page: page,
    where: { state },
    like: { title, propertyId },
  });
  const resolveData = await resolveDataFun(req, newData);
  let count = await req.services.HousesService.getCount({
    where: { state },
    like: { title, propertyId },
  });

  res.status(200).json({
    data: {
      list: resolveData,
      total: count,
    },
    msg: "success",
    code: 200,
  });
};

exports.del = async (req, res) => {
  const body = req.query;
  const { id } = body;
  const newData = await req.services.HousesService.delete(Number(id));
  if (newData.affectedRows === 1) {
    res.status(200).json({ data: "删除成功", msg: "success", code: 200 });
  } else {
    res.status(200).json({ data: "查无数据", msg: "fail", code: 400 });
  }
};
